package leecode.duplicate;

import java.util.Arrays;
import java.util.HashSet;

public class Solution {

    public int lengthOfLongestSubstring(String s) {
        int max = 0;
        int start = 0;
        int end = 1;
        if (s.length() <= 1) {
            return s.length();
        }
        while (end != s.length() + 1) {
            String sub = s.substring(start, end);
            if (hasDupChar(sub)) {
                start++;
            } else {
                if (end - start > max) {
                    max = end - start;
                }
                end++;
            }
        }
        //"abcabcbb"
        return max;
    }

    public boolean hasDupChar(String s) {
        HashSet<Character> hs = new HashSet<>();
        for (int i = 0; i < s.length(); i++) {
            if (hs.contains(s.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    public boolean hasDupChar1(String s) {
        int num = 0;
        for (char c : s.toCharArray()) {
            int cur = c - 'a';
            if ((num & 1 << cur) != 0) {
                return true;
            } else {
                num = num | 1 << cur;
            }
        }
        return false;
    }

    boolean hasDuplicateChars(String s) {
        // 初始化一个hashSet，因为不能含有重复元素
        HashSet<Character> hashSet = new HashSet<>();
        for (int i = 0; i < s.length(); i++) {
            // 遍历每一个字符，如果已经包含了，则直接返回是重复串
            if (hashSet.contains(s.charAt(i))) {
                return true;
            } else {
                hashSet.add(s.charAt(i));
            }
        }
        return false;
    }

    public boolean CheckPermutation(String s1, String s2) {
        if (s1.length() != s2.length()) {
            return false;
        }
        int aa = 0;
        for (int i = 0; i < s1.length(); i++) {
            aa += (1 << (s1.charAt(i) - 'a')) - (1 << (s2.charAt(i) - 'a'));
        }
        return aa == 0;
    }

    public boolean CheckPermutation1(String s1, String s2) {
        char[] chars1 = s1.toCharArray();
        char[] chars2 = s2.toCharArray();
        Arrays.sort(chars1);
        Arrays.sort(chars2);
        return new String(chars1).equals(new String(chars2));
    }

    public String CheckPermutation2(String S, int length) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            char c = S.charAt(i);
            stringBuilder.append(c == ' ' ? "%20" : c);
        }
        return stringBuilder.toString();
    }

    public String CheckPermutation3(String S, int length) {
        char[] chars = new char[S.length()];
        int j = 0;
        for (int i = 0; i < length; i++) {
            char c = S.charAt(i);
            if (c == ' ') {
                chars[j] = '%';
                chars[j + 1] = '2';
                chars[j + 2] = '0';
                j = j + 3;
            } else {
                chars[j++] = c;
            }
        }
        return new String(chars).trim();
    }

    public String CheckPermutation4(String S, int length) {
        char[] chars = S.toCharArray();
        int index = chars.length - 1;
        for (int i = length - 1; i >= 0; i--) {
            if (chars[i] == ' ') {
                chars[index--] = '0';
                chars[index--] = '2';
                chars[index--] = '%';
            } else {
                chars[index--] = chars[i];
            }
        }
        return new String(chars).substring(index + 1);
    }

    public boolean oneEditAway(String first, String second) {
        int lenf = first.length();
        int lens = second.length();
        int a = lenf - lens;
        if (Math.abs(a) >= 2) {
            return false;
        }
        if (first.equals(second)) {
            return true;
        }
        boolean firstLong = a > 0;
        String longString = firstLong ? first : second;
        String shortString = firstLong ? second : first;
        if (longString.length() <= 1) {
            return true;
        }
        int diff = 0;
        for (int i = 0; i < longString.length(); i++) {
            if (i == shortString.length()) {
                return true;
            }
            if (diff >= 2) {
                return false;
            }
            char c1 = longString.charAt(i);
            char c2 = shortString.charAt(lenf - lens != 0 ? i - diff : i);
            if (c1 == c2) {
            } else {
                diff++;
                if (lenf - lens == 0) {
                    continue;
                }
                c1 = longString.charAt(++i);
                if (c1 == c2) {
                } else {
                    diff++;
                }
            }
        }
        return diff <= 1;
    }
}
